﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MapCenterSystem.DefineType
{
    public class Coordinate
    {
        double longitude;
        double latitude;

        public double Longitude
        {
            get { return longitude; }
            set { longitude = value; }
        }

        public double Latitude
        {
            get { return latitude; }
            set { latitude = value; }
        }

        public Coordinate()
        {
            longitude = 0;
            latitude = 0;
        }

        public Coordinate(Coordinate p)
        {
            this.latitude = p.latitude;
            this.longitude = p.longitude;
        }

        public Coordinate(double latitude,double longitude)
        {
            this.latitude = latitude;
            this.longitude = longitude;
        }

        public override bool Equals(object obj)
        {
            Coordinate p = (Coordinate)obj;
            if (p.Longitude == this.Longitude && p.Latitude == this.Latitude)
                return true;
            return false;
        }

        public static double Distance(Coordinate p1, Coordinate p2)
        {
            double theta = p1.Longitude - p2.Longitude;
            double dist = Math.Sin(deg2rad(p1.Latitude)) * Math.Sin(deg2rad(p2.Latitude)) + Math.Cos(deg2rad(p1.Latitude)) * Math.Cos(deg2rad(p2.Latitude)) * Math.Cos(deg2rad(theta));
            dist = Math.Acos(dist);
            dist = rad2deg(dist);
            dist = dist * 60 * 1.1515;
            dist = dist * 1.609344;

            return (dist);
        }

        //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        //::  This function converts decimal degrees to radians             :::
        //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        public static double deg2rad(double deg)
        {
            return (deg * Math.PI / 180.0);
        }

        //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        //::  This function converts radians to decimal degrees             :::
        //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        public static double rad2deg(double rad)
        {
            return (rad / Math.PI * 180.0);
        }
    }
}
